﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entities;

namespace Stationery
{
    public partial class Login : System.Web.UI.Page
    {
        BLL_User bll_User = new BLL_User();

        protected void Page_Load(object sender, EventArgs e)
        {
            Session[Utils.SESSION_ADVERTISE] = null;
            Session[Utils.SESSION_CART] = null;
            Session[Utils.SESSION_CART_ADMIN] = null;
            Session[Utils.SESSION_DATA] = null;
            Session[Utils.SESSION_LOGIN] = null;
            Session[Utils.SESSION_PRODUCT] = null;
            Session[Utils.SESSION_USER] = null;

            txtUsername.Focus();

            if (!IsPostBack)
            {
                TurnOnCaptcha(false);
            }
        }

        protected void ImageButtonLogin_Click(object sender, ImageClickEventArgs e)
        {
            string strUser = txtUsername.Text.Trim();
            string strPass = EncPass.EncryptString(txtPassword.Text.Trim(), Utils.SHARE_SECRETKEY);

            if (CaptchaControl1.Visible == true)
            {
                CaptchaControl1.ValidateCaptcha(txtConfirm.Text);
                if (!CaptchaControl1.UserValidated)
                {
                    CaptchaControl1.CustomValidatorErrorMessage = "Mã xác nhận không đúng";
                    txtConfirm.Text = "";
                    return;
                }
            }

            if (bll_User.IsExistUser(strUser, strPass) == false)
            {
                lblError.Text = "Tên đăng nhập hoặc mật khẩu không đúng.";
                TurnOnCaptcha(true);
            }
            else
            {
                Entities.User user = bll_User.Lst_User(bll_User.Get_User_ByUserPass(strUser, strPass))[0];

                if (user != null)
                {
                    Session[Utils.SESSION_LOGIN]   = user;
                    Session[Utils.SESSION_CAPTCHA] = null;

                    Response.Redirect(UtilsURL.admin);
                }
                else
                {
                    Response.Redirect(UtilsURL.home);
                }
            }
        }

        private void TurnOnCaptcha(bool pIncre)
        {
            // pIncre = true : nhap sai ma xac nhan --> tang số đếm lên
            // pIncre = false: khởi tạo ban đầu
            if (Session[Utils.SESSION_CAPTCHA] == null)
            {
                if (pIncre == true)
                {
                    Session[Utils.SESSION_CAPTCHA] = 1;
                }
                else
                {
                    controlCaptcha.Visible        = false;
                    controlConfirmCaptcha.Visible = false;
                }
            }
            else
            {
                Session[Utils.SESSION_CAPTCHA] = Convert.ToInt16(Session[Utils.SESSION_CAPTCHA]) + 1;

                if (Convert.ToInt16(Session[Utils.SESSION_CAPTCHA]) > 2)
                {
                    controlCaptcha.Visible = true;
                    controlConfirmCaptcha.Visible = true;
                }
                else
                {
                    controlCaptcha.Visible = false;
                    controlConfirmCaptcha.Visible = false;
                }
            }
        }
    }
}